﻿@using HCMS.Dictionary;
@{
    Layout = "~/Views/Shared/_Layout_Edit_Pear_Layui.cshtml";

    var provinceDic = new Dictionary<string, string>() {
        { "北京市", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B1%B1%BE%A9%CA%D0%A3%A8%BE%A9%A3%A9&diji=-1&xianji=-1"},
        { "天津市", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%CC%EC%BD%F2%CA%D0%A3%A8%BD%F2%A3%A9&diji=-1&xianji=-1" },
        { "河北省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%D3%B1%B1%CA%A1%A3%A8%BC%BD%A3%A9&diji=-1&xianji=-1" },
        { "山西省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C9%BD%CE%F7%CA%A1%A3%A8%BD%FA%A3%A9&diji=-1&xianji=-1" },
        { "内蒙古自治区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C4%DA%C3%C9%B9%C5%D7%D4%D6%CE%C7%F8%A3%A8%C4%DA%C3%C9%B9%C5%A3%A9&diji=-1&xianji=-1" },
        { "辽宁省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C1%C9%C4%FE%CA%A1%A3%A8%C1%C9%A3%A9&diji=-1&xianji=-1" },
        { "吉林省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BC%AA%C1%D6%CA%A1%A3%A8%BC%AA%A3%A9&diji=-1&xianji=-1" },
        { "黑龙江省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%DA%C1%FA%BD%AD%CA%A1%A3%A8%BA%DA%A3%A9&diji=-1&xianji=-1" },
        { "上海市", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C9%CF%BA%A3%CA%D0%A3%A8%BB%A6%A3%A9&diji=-1&xianji=-1" },
        { "江苏省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BD%AD%CB%D5%CA%A1%A3%A8%CB%D5%A3%A9&diji=-1&xianji=-1" },
        { "浙江省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%D5%E3%BD%AD%CA%A1%A3%A8%D5%E3%A3%A9&diji=-1&xianji=-1" },
        { "安徽省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B0%B2%BB%D5%CA%A1%A3%A8%CD%EE%A3%A9&diji=-1&xianji=-1" },
        { "福建省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B8%A3%BD%A8%CA%A1%A3%A8%C3%F6%A3%A9&diji=-1&xianji=-1" },
        { "江西省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BD%AD%CE%F7%CA%A1%A3%A8%B8%D3%A3%A9&diji=-1&xianji=-1" },
        { "山东省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C9%BD%B6%AB%CA%A1%A3%A8%C2%B3%A3%A9&diji=-1&xianji=-1" },
        { "河南省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%D3%C4%CF%CA%A1%A3%A8%D4%A5%A3%A9&diji=-1&xianji=-1" },
        { "湖北省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%FE%B1%B1%CA%A1%A3%A8%B6%F5%A3%A9&diji=-1&xianji=-1" },
        { "湖南省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%FE%C4%CF%CA%A1%A3%A8%CF%E6%A3%A9&diji=-1&xianji=-1" },
        { "广东省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B9%E3%B6%AB%CA%A1%A3%A8%D4%C1%A3%A9&diji=-1&xianji=-1" },
        { "广西壮族自治区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B9%E3%CE%F7%D7%B3%D7%E5%D7%D4%D6%CE%C7%F8%A3%A8%B9%F0%A3%A9&diji=-1&xianji=-1" },
        { "海南省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%BA%A3%C4%CF%CA%A1%A3%A8%C7%ED%A3%A9&diji=-1&xianji=-1" },
        { "重庆市", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%D6%D8%C7%EC%CA%D0%A3%A8%D3%E5%A3%A9&diji=-1&xianji=-1" },
        { "四川省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%CB%C4%B4%A8%CA%A1%A3%A8%B4%A8%A1%A2%CA%F1%A3%A9&diji=-1&xianji=-1" },
        { "贵州省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B9%F3%D6%DD%CA%A1%A3%A8%C7%AD%A1%A2%B9%F3%A3%A9&diji=-1&xianji=-1" },
        { "云南省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%D4%C6%C4%CF%CA%A1%A3%A8%B5%E1%A1%A2%D4%C6%A3%A9&diji=-1&xianji=-1" },
        { "西藏自治区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%CE%F7%B2%D8%D7%D4%D6%CE%C7%F8%A3%A8%B2%D8%A3%A9&diji=-1&xianji=-1"},
        { "陕西省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C9%C2%CE%F7%CA%A1%A3%A8%C9%C2%A1%A2%C7%D8%A3%A9&diji=-1&xianji=-1" },
        { "甘肃省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B8%CA%CB%E0%CA%A1%A3%A8%B8%CA%A1%A2%C2%A4%A3%A9&diji=-1&xianji=-1" },
        { "青海省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C7%E0%BA%A3%CA%A1%A3%A8%C7%E0%A3%A9&diji=-1&xianji=-1" },
        { "宁夏回族自治区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%C4%FE%CF%C4%BB%D8%D7%E5%D7%D4%D6%CE%C7%F8%A3%A8%C4%FE%A3%A9&diji=-1&xianji=-1" },
        { "新疆维吾尔自治区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%D0%C2%BD%AE%CE%AC%CE%E1%B6%FB%D7%D4%D6%CE%C7%F8%A3%A8%D0%C2%A3%A9&diji=-1&xianji=-1" },
        { "香港特别行政区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%CF%E3%B8%DB%CC%D8%B1%F0%D0%D0%D5%FE%C7%F8%A3%A8%B8%DB%A3%A9&diji=-1&xianji=-1" },
        { "澳门特别行政区", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%B0%C4%C3%C5%CC%D8%B1%F0%D0%D0%D5%FE%C7%F8%A3%A8%B0%C4%A3%A9&diji=-1&xianji=-1" },
        { "台湾省", "http://xzqh.mca.gov.cn/defaultQuery?shengji=%CC%A8%CD%E5%CA%A1%A3%A8%CC%A8%A3%A9&diji=-1&xianji=-1" }
    };
}

<div class="layui-body layui-bg-gray">
    <div class="layui-card">
        <form class="layui-form">

            <div class="layui-tab layui-tab-card">
                <ul class="layui-tab-title">
                    <li class="layui-this">民政部数据</li>
                </ul>

                <div class="layui-tab-content">
                    <!-- 民政部数据 -->
                    <div class="layui-tab-item layui-show">

                        <div class="layui-form-item">
                            <label class="layui-form-label"></label>
                            <div class="layui-input-inline" style="width:230px;">
                                <input type="checkbox" title="全选" lay-filter="all-checkbox-filter" checked />
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">省份</label>
                            <div class="layui-input-inline" style="width:80%;">
                                @{
                                    var i = 0;
                                    foreach (var item in provinceDic)
                                    {
                                        <input type="checkbox" name="province" value="@item.Value" title="@item.Key" checked />

                                        if (i > 0 && i % 5 == 0)
                                        {
                                            <br />
                                        }

                                        i++;
                                    }
                                }
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">数据来源</label>
                            <div class="layui-form-label content-label" style="width:250px;">
                                <a href="http://xzqh.mca.gov.cn/map" target="_blank" style="color:blue;">全国行政区划信息查询平台</a>
                            </div>
                        </div>

                    </div>

                </div>
            </div>

            <div class="layui-form-item layui-fixbar btn-fixbar-box">
                <div class="layui-input-block">
                    <vc:menu-submit action-names="@(new string[]{"crawlData"})"></vc:menu-submit>
                    <button type="button" class="layui-btn layui-btn-primary layui-border-red" lay-on="close">
                        <i class="layui-icon layui-icon-close"></i>关闭
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>

@section Scripts {
    <script>
        layui.use(['form', 'jquery'], function () {
            let form = layui.form;
            let $ = layui.jquery;
            var layer = layui.layer;

            allSelect();
            submitFn();

            // 全选/全不选
            function allSelect(){
                var $province = $('input[name="province"]');

                form.on('checkbox(all-checkbox-filter)', function(data){
                      var elem = data.elem;
                      var checked = elem.checked;

                      if(checked) {
                        $province.prop('checked', true);
                      } else {
                        $province.prop('checked', false);
                      }
                });
            }

            // 表单提交
            function submitFn() {
                var areaData = [];
                var analysisSuccessCount = 0;

                form.on('submit(crawlData)', function (data) {
                    areaData = [];
                    getCheckedInfo();
                    return false;
                });

                // 1、获取选中省份数据
                function getCheckedInfo(){
                    var checkedCount = $('input[name="province"]:checked').length;
                    if(checkedCount === 0){
                        layer.msg('请选择要抓取数据的省份', { icon: 2 });
                        return false;
                    }

                    layer.msg('开始处理', {
                      icon: 16,
                      shade: 0.1,
                      time: 0
                    });

                    var $province = $('input[name="province"]');
                    for(var i = 0; i < $province.length; i++){
                        var $item = $($province[i]);

                        if(!$item.prop('checked')){ continue; }

                        getHtml($item.attr('title'), $item.val());
                    }

                    // 全部解析完成，才提交
                    var timeObj = setInterval(function(){
                        if(analysisSuccessCount > 0 && analysisSuccessCount === checkedCount){
                            layer.msg('数据解析完成，正在保存', {
                                icon: 16,
                                shade: 0.1,
                                time: 0
                            });

                            ajaxSubmit(areaData);
                            clearInterval(timeObj);
                        }
                    },200);
                }

                // 2、获取 Html
                function getHtml(provinceName, webUrl){
                    $.ajax({
                        url: '/systemarea/analysishtml/',
                        type: 'post',
                        dataType: 'json',
                        data: { webUrl: webUrl },
                        success: function (result) {
                            if (result.code === 200) {
                                htmlHandle(provinceName, result.result);
                                analysisSuccessCount++;
                            } else {
                                layer.msg(result.message, { icon: 2 })
                            }

                        }
                    });
                }

                // 3、解析 Html
                function htmlHandle(provinceName, htmlInfo){
                    var cityArray = [];
                    var areaArray = [];
                    var $html =$(htmlInfo);

                    var isSpecialZone = false;
                    if(['香港特别行政区','澳门特别行政区','台湾省'].indexOf(provinceName) > -1) {
                        isSpecialZone = true;
                    }

                    // 行政区划表格行
                    var $list = $html.find('.info_table tbody tr');
                    if($list.length >= 1 && !isSpecialZone) {
                        // 获取表格行信息
                        for(var i = 1; i < $list.length; i++){
                            var $item = $($list[i]);

                            // 城市数据
                            if($item.hasClass('shi_nub')){
                                var $city = $item;
                                var cityName = $city.find('.name_left input[name="hid"]').val();
                                var regionalismCode = $city.find('td').eq(4).text();
                                var areaCode = $city.find('td').eq(5).text();
                                var postCode = $city.find('td').eq(6).text();

                                cityArray.push({
                                    cityName: cityName,
                                    regionalismCode: regionalismCode || 0,
                                    areaCode: areaCode || '',
                                    postCode: postCode || 0
                                });

                            } else {    // 区域数据
                                var $area = $item;
                                var cityName = $area.attr('parent');
                                var areaName = $area.find('.name_left input[name="hidzxs"]').attr('alt');
                                var regionalismCode = $area.find('td').eq(4).text();
                                var areaCode = $area.find('td').eq(5).text();
                                var postCode = $area.find('td').eq(6).text();

                                areaArray.push({
                                    cityName: cityName,
                                    areaName: areaName,
                                    regionalismCode: regionalismCode || 0,
                                    areaCode: areaCode || '',
                                    postCode: postCode || 0
                                });
                            }
                        }
                    }

                    areaData.push({
                        provinceName: provinceName,
                        cityList: cityArray,
                        areaList: areaArray
                    });
                }

                // 4、提交数据
                function ajaxSubmit(areaData){
                    $.ajax({
                        url: '/systemarea/crawldata/',
                        type: 'post',
                        dataType: 'json',
                        data: { areaData: areaData },
                        success: function (result) {
                            if (result.code === 200) {
                                layer.closeAll();

                                layer.msg(result.message, { icon: 1, time: 1000 }, function () {
                                    parent.layer.close(parent.layer.getFrameIndex(window.name)); // 关闭当前页

                                    parent.layui.table.reload("tableList", {
                                        page: { curr: 1 },
                                    });

                                    window.parent.loadTreeData();

                                    analysisSuccessCount = 0;
                                });
                            } else {
                                parent.layer.msg(result.message, { icon: 2 });
                            }
                        }
                    });
                }

            }
        });
    </script>
}
